package com.niit.galuser.controller;

import com.niit.entity.User;
import com.niit.galuser.service.impl.UserServiceImpl;
import com.niit.utils.CommonResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("register")
public class RegisterController {
    @Resource
    private UserServiceImpl userService;

    @PostMapping("bymail")
    public CommonResult register(HttpSession session,@RequestBody User user1){
        String addr = user1.getEmail();
        User user = userService.findUserByMail(addr);
        System.out.println(session.getAttribute(addr));
        if (user!=null)
            return new CommonResult(401,"用户已存在");
        else if (!user1.getCode().equals((String) session.getAttribute(addr)))
            return new CommonResult(401,"验证码错误");

        user = new User();
        user.setEmail(addr);
        user.setPassword(user1.getPassword());
        userService.insertUser(user);
        return new CommonResult(200,"注册成功");
    }
}
